package com.yz.newspaper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yz.newspaper.mapper.LiteratureTypeMapper;
import com.yz.newspaper.pojo.po.LiteratureType;
import com.yz.newspaper.service.ILiteratureTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 体裁类型表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-21
 */
@Service
public class LiteratureTypeServiceImpl extends ServiceImpl<LiteratureTypeMapper, LiteratureType> implements ILiteratureTypeService {

    @Override
    public Map<Integer, List<LiteratureType>> getLiteratureTypes() {
        LambdaQueryWrapper<LiteratureType> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(LiteratureType::getMedia).groupBy(LiteratureType::getMedia);
        List<LiteratureType> media = baseMapper.selectList(wrapper);
        Map<Integer, List<LiteratureType>> maps = new HashMap<>();
        media.forEach(item -> {
            List<LiteratureType> literatureTypes
                    = baseMapper.selectList(new LambdaQueryWrapper<LiteratureType>()
                    .eq(LiteratureType::getMedia, item.getMedia()));
            maps.put(item.getMedia(), literatureTypes);
        });

        return maps;
    }

    @Override
    public List<LiteratureType> getLiteratureTypesByMedia(String media) {
        List<LiteratureType> literatureTypes = baseMapper.selectList(new LambdaUpdateWrapper<LiteratureType>()
                .eq(LiteratureType::getMedia, media));

        return literatureTypes;
    }
}
